

package com.weavernorth.humanresource;



import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weaver.general.BaseBean;
import weaver.general.Util;

import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Iterator;
import java.util.List;


//销售提成办公室人员推送SAP
public class HRXstcBgsryAction implements Action {
//    public static Log log = LogFactory.getLog(HRXstcBgsryAction.class.getName());
private Logger log = Logger4jUtil.getLoggerByName("/action/humanresource/");
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb = new BaseBean();

        //主表数据库
        RecordSet rs = new RecordSet();
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //提示信息
        String strMessage = "";

        String testType = "";

        boolean isSuccess = false;
        //获取当前日期
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_MONTH);
        String requestid = requestInfo.getRequestid();//请求ID
        String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        log.info("人事-销售提成-办公室人员tablename的值是:" + tablename + "\n requestid" + requestid);


        //主表数据库字段
        String rq = "";//日期

        double rw = 0.0;//任务-分公司销售任务

        double zjyj = 0.0;//正价业绩-分公司销售业绩
        double zjyj1 = 0.0;//折价业绩

        double wcl = 0.0;//完成率


        RecordSet mbrs = new RecordSet();//码表rs
        String mbsql = "";  //查询码表sql

        //1.获取主表单数据
        String sql = " select * from " + tablename + " where requestid =" + requestid;
        log.info("人事-销售提成-办公室人员接口sql的值是:" + sql + "\n");
        rs.execute(sql);

        if (rs.next()) {

            rq = rs.getString("czrq");//生效日期

            rw = Util.getDoubleValue(rs.getString("fgsxsrw"),0.0);//任务-分公司销售任务

            zjyj = Util.getDoubleValue(rs.getString("fgsxsyj"),0.0);//正价业绩-分公司销售业绩


            wcl = Util.getDoubleValue(rs.getString("wcl"),0.0);//完成率


            log.info("人事-销售提成-办公室人员:日期sxrq的值是:" + rq + "\n");


        }
        BigDecimal b_rw=new BigDecimal(rw);
        BigDecimal b_zjyj=new BigDecimal(zjyj);

        //2.封装sap请求xml
        StringBuffer xml = new StringBuffer("");


        xml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">");


        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body>");

        xml.append("<urn:ZHRI_077>");

        //Return字段
        xml.append("<RETURN>");
        xml.append("<item>");
        xml.append("<TYPE></TYPE>");
        xml.append("<ID></ID>");
        xml.append("<NUMBER></NUMBER>");
        xml.append("<MESSAGE></MESSAGE>");
        xml.append("<LOG_NO></LOG_NO>");
        xml.append("<LOG_MSG_NO></LOG_MSG_NO>");
        xml.append("<MESSAGE_V1></MESSAGE_V1>");
        xml.append("<MESSAGE_V2></MESSAGE_V2>");
        xml.append("<MESSAGE_V3></MESSAGE_V3>");
        xml.append("<MESSAGE_V4></MESSAGE_V4>");
        xml.append("<PARAMETER></PARAMETER>");
        xml.append("<ROW></ROW>");
        xml.append("<FIELD></FIELD>");
        xml.append("<SYSTEM></SYSTEM>");
        xml.append("</item>");
        xml.append("</RETURN>");


        xml.append("<T_9135>");


        //子表数据

        RecordSet dt1rs = new RecordSet();

        //1.获取子表单dt1数据
        String dt1sql = " select  * from " + tablename + "_dt1 dt1 " + " where dt1.mainid=( select id from " + tablename + " mt where mt.requestid =" + requestid + ")";

        log.info("人事-销售提成-办公室人员接口dt1sql的值是:" + dt1sql + "\n");
        dt1rs.execute(dt1sql);

        while (dt1rs.next()) {


            //****************销售提成-零售经理***************************


            //获取数据
            //String  ygbh=dt1rs.getString("ygbh");//员工编号


            double zjjj = 0.0;//正价奖金
            double zjjj1 = 0.0;//折价奖金

            double zjj =Util.getDoubleValue(dt1rs.getString("tcje"),0.0);//总奖金
            BigDecimal b_zjj=new BigDecimal(zjj);
            String tcrq = rq;//提成日期


            //CLOB读取

            //************CLOB读取***************

            String clob = dt1rs.getString("ygxm");

            log.info("人事-销售提成-办公室人员:Clob的值:" + clob + "\n");

            List<String> ClobList = Arrays.asList(clob.split(","));


            for (int i = 0; i < ClobList.size(); i++) {

                String id = ClobList.get(i);

                log.info("Clob的值的元素是" + id + "\n");






                //转码
                //****************员工工号**************************

                String ygbh = ""; //工号
                mbsql = "select workcode from HRMRESOURCE t where id = " + id;


                mbrs.execute(mbsql);
                if (mbrs.next()) {

                    ygbh = mbrs.getString("workcode");


                    //xml.append("<xx>" + xx + "</xxx>");

                    log.info("员工工号ygbh sql的值:" + mbsql + "\n");
                    log.info("员工工号ygbh 的值:" + ygbh + "\n");
                }



                //zm 增加,判断工资范围是否锁定 开始
                String strGzfw=salarRangeUtil.getGzfwFromUser(id);

                String strLockMess = salarRangeUtil.judgeLock(strGzfw, rq);
                log.info("工资核算范围返回信息:" + strLockMess);
                if (!"".equals(strLockMess)) {
                    strLockMess="工号为:"+ygbh+","+strLockMess+"薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                    requestInfo.getRequestManager().setMessagecontent(strLockMess);
                    return FAILURE_AND_CONTINUE;

                }
                //zm 增加,判断工资范围是否锁定 结束






                //********************************************


//                //子表数据
//                xml.append("<item>");
//                xml.append("<PERNR>"+ygbh+"</PERNR>");//员工编号(子表)
//
//                xml.append("<ZRWJE>"+rw+"</ZRWJE>");//任务(子表)
//                xml.append("<ZZJJE>"+zjyj+"</ZZJJE>");//正价业绩(子表)
//                xml.append("<ZZJJE1>"+zjyj1+"</ZZJJE1>");//折价业绩(子表)
//                xml.append("<ZPER>"+wcl+"</ZPER>");//完成率(子表)
//                xml.append("<ZZJJJ>"+zjjj+"</ZZJJJ>");//正价奖金(子表)
//                xml.append("<ZZJJJ1>"+zjjj1+"</ZZJJJ1>");//折价奖金(子表)
//                xml.append("<ZJJ>"+zjj+"</ZJJ>");//总奖金(子表)
//                xml.append("<ZDATE>"+tcrq+"</ZDATE>");//提成日期(子表)
//
//                xml.append("</item>");


                //子表数据
                xml.append("<item>");
                xml.append("<PERNR>"+ygbh+"</PERNR>");//员工编号(子表)
               // xml.append("<Pernr>" + ygbh + "</Pernr>");//员工编号(子表)
                b_rw=b_rw.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入，保留两位
                xml.append("<ZRWJE>"+b_rw+"</ZRWJE>");//任务(子表)
               // xml.append("<Zrwje>" + rw + "</Zrwje>");//任务(子表)
                //xml.append("<ZZJJE>"+zjyj+"</ZZJJE>");//正价业绩(子表)
                b_zjyj=b_zjyj.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入，保留两位
                xml.append("<Zzjje>" + b_zjyj + "</Zzjje>");//正价业绩(子表)

                BigDecimal b_zjyj1=new BigDecimal(zjyj1);
                b_zjyj1=b_zjyj1.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入，保留两位
                xml.append("<ZZJJE1>"+b_zjyj1+"</ZZJJE1>");//折价业绩(子表)
               // xml.append("<Zzjje1>" + zjyj1 + "</Zzjje1>");//折价业绩(子表)
                xml.append("<ZPER>"+wcl+"</ZPER>");//完成率(子表)
                //xml.append("<Zper>" + wcl + "</Zper>");//完成率(子表)

                BigDecimal b_zjjj=new BigDecimal(zjjj);
                b_zjjj=b_zjjj.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入，保留两位
                xml.append("<ZZJJJ>"+b_zjjj+"</ZZJJJ>");//正价奖金(子表)
                //xml.append("<Zzjjj>" + zjjj + "</Zzjjj>");//正价奖金(子表)

                //xml.append("<ZZJJJ1>"+zjjj1+"</ZZJJJ1>");//折价奖金(子表)
                BigDecimal b_zjjj1=new BigDecimal(zjjj1);
                b_zjjj1=b_zjjj1.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入，保留两位
                xml.append("<Zzjjj1>" + b_zjjj1 + "</Zzjjj1>");//折价奖金(子表)
                b_zjj=b_zjj.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入，保留两位
                xml.append("<ZJJ>"+b_zjj+"</ZJJ>");//总奖金(子表)
                //xml.append("<Zjj>" + zjj + "</Zjj>");//总奖金(子表)


                xml.append("<ZDATE>"+tcrq+"</ZDATE>");//提成日期(子表)
               // xml.append("<Zdate>" + tcrq + "</Zdate>");//提成日期(子表)

                xml.append("</item>");

            }


        }


        xml.append("</T_9135>");

        xml.append("</urn:ZHRI_077>");
        //xml.append("</urn:Zhri077>");


        xml.append("</soapenv:Body>");
        xml.append("</soapenv:Envelope>");


        log.info("人事-销售提成-办公室人员接口XML的值是:" + xml.toString());


        //CommonToSapUtil commonToSapUtil=new CommonToSapUtil();
        try {
            //3.推送数据


            String txurl = Util.null2String(bb.getPropValue("personnel-erdos", "xstcurl"));

            String responseXml = salarRangeUtil.sendPost(txurl, xml.toString(), "utf-8");

            log.info("人事-销售提成-办公室人员接SAP返回的值是:" + responseXml.toString());

            //===========SAP 返回解析====================

            String strWorkcode = "";

            //xmlStr为上图格式的字符串


            String EPernr = "";//员工编码

            Document doc = null;
            try {
                doc = DocumentHelper.parseText(responseXml.toString());
                Iterator<Element> its2 = doc.selectNodes("//TYPE").iterator();
                while (its2.hasNext()) {
                    Element element = its2.next();
                    testType = element.getText();
                    //System.out.println(testType);
                    if (!"".equals(testType)) {
                        isSuccess = true;
                        log.info("推送成功testType值为:" + testType + "\n");
                        break;
                    }


                }


                //返回值失败时,则提示失败
                if ("E".equals(testType)) {
                    Iterator<Element> its3 = doc.selectNodes("//MESSAGE").iterator();
                    while (its3.hasNext()) {
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            //根据返回值是否提交流程
            if ("S".equals(testType)) {
                return SUCCESS;
            } else {
                if (!"".equals(strMessage)) {
                    requestInfo.getRequestManager().setMessagecontent(strMessage);
                }
                return FAILURE_AND_CONTINUE;
            }





        } catch (Exception e) {
            e.printStackTrace();
            return FAILURE_AND_CONTINUE;
        }


        //return SUCCESS;
    }
}












































